import { create } from 'zustand'
import { goodsService } from "@/services/goodsService";
import { message } from 'antd';
import type { List } from '@/types/common'
import type { GoodsItem } from "../types/goods.d.ts"

export const useGoodsStore = create((set) => ({
    goodsData: {} as List<GoodsItem>,

    /**  查询 */
    getGoods: async (params = {}) => {

        // this.goodsData = {} as List<GoodsItem>
        set(() => ({ goodsData: {} as List<GoodsItem> }))

        const { code, data } = await goodsService.getGoodesService(params)

        if (code) {
            message.error(`查询失败`)
        } else {
            // this.goodsData = data
            set(() => ({ goodsData: data as List<GoodsItem> }))
        }

    },

    /**  增加 */
    createGoods: async (params: GoodsItem) => {
        const { code } = await goodsService.createGoodsService(params)

        if (code) {
            message.error(`添加失败`)
        } else {
            message.success(`添加成功`)
        }

        return { code }

    },

    /**  更新 */
    updateGoods: async (params: GoodsItem) => {
        const { code } = await goodsService.updateGoodsService(params)

        if (code) {
            message.error(`更新失败`)
        } else {
            message.success(`更新成功`)
        }

        return { code }

    },

    /**  删除 */
    deleteGoods: async (params: { productId: string | number }) => {

        const { code } = await goodsService.deleteGoodsService(params)

        if (code) {
            message.error(`删除失败`)
        } else {
            message.success(`删除成功`)
        }

        return { code }

    }




}))

// export const goodsStore = defineStore('goodsStore', {
//     state: () => ({
//         goodsData: {} as List<GoodsItem>
//     }),

//     actions: {
//         /**  查询 */
//         async getGodes(params = {}) {
//             this.goodsData = {} as List<GoodsItem>

//             const { code, data } = await goodsService.getGoodesService(params)

//             if (code) {
//                 ElMessage.error('查询失败.')
//             } else {
//                 this.goodsData = data
//             }

//         },

//         /**  删除 */
//         async deleteGoods(params: { productId: string | number }) {

//             const { code } = await goodsService.deleteGoodsService(params)

//             ElMessage({
//                 // message: `删除${message}`,
//                 message: `删除${code ? '失败' : '成功'}`,
//                 type: code ? 'warning' : 'success',
//             })

//             return { code }

//         },



//         /**  增加 */
//         async createGoods(params: GoodsItem) {
//             const { code } = await goodsService.createGoodsService(params)

//             ElMessage({
//                 // message: `添加${message}`,
//                 message: `添加${code ? '失败' : '成功'}`,
//                 type: code ? 'warning' : 'success',
//             })

//             return { code }

//         },

//         /**  更新 */
//         async updateGoods(params: GoodsItem) {
//             const { code } = await goodsService.updateGoodsService(params)

//             ElMessage({
//                 // message: `更新${message}`,
//                 message: `更新${code ? '失败' : '成功'}`,
//                 type: code ? 'warning' : 'success',
//             })

//             return { code }

//         }
//     }
// })